#ifndef EVENT_BUS_H
#define EVENT_BUS_H
#include <string>
#include <memory>
#include <mutex>
#include <queue>
#include <cstdlib>
#include <cstring>
#include "macro.h"

struct Event {
    int   id;
    char* payload;   // 动态分配的内存
    size_t payload_size;  // payload大小
    
    // 构造函数
    Event(int event_id, const char* data, size_t size) : id(event_id), payload_size(size) {
        if (data && size > 0) {
            payload = (char*)malloc(size);
            if (payload) {
                memcpy(payload, data, size);
            } else {
                payload = nullptr;
                payload_size = 0;
            }
        } else {
            payload = nullptr;
            payload_size = 0;
        }
    }
    
    // 默认构造函数
    Event() : id(0), payload(nullptr), payload_size(0) {}
    
    // 拷贝构造函数
    Event(const Event& other) : id(other.id), payload_size(other.payload_size) {
        if (other.payload && other.payload_size > 0) {
            payload = (char*)malloc(payload_size);
            if (payload) {
                memcpy(payload, other.payload, payload_size);
            } else {
                payload = nullptr;
                payload_size = 0;
            }
        } else {
            payload = nullptr;
            payload_size = 0;
        }
    }
    
    // 赋值操作符
    Event& operator=(const Event& other) {
        if (this != &other) {
            // 释放现有资源
            if (payload) {
                free(payload);
            }
            
            id = other.id;
            payload_size = other.payload_size;
            
            if (other.payload && other.payload_size > 0) {
                payload = (char*)malloc(payload_size);
                if (payload) {
                    memcpy(payload, other.payload, payload_size);
                } else {
                    payload = nullptr;
                    payload_size = 0;
                }
            } else {
                payload = nullptr;
                payload_size = 0;
            }
        }
        return *this;
    }
    
    // 移动构造函数
    Event(Event&& other) noexcept : id(other.id), payload(other.payload), payload_size(other.payload_size) {
        other.id = 0;
        other.payload = nullptr;
        other.payload_size = 0;
    }
    
    // 移动赋值操作符
    Event& operator=(Event&& other) noexcept {
        if (this != &other) {
            // 释放现有资源
            if (payload) {
                free(payload);
            }
            
            id = other.id;
            payload = other.payload;
            payload_size = other.payload_size;
            
            other.id = 0;
            other.payload = nullptr;
            other.payload_size = 0;
        }
        return *this;
    }
    
    // 析构函数
    ~Event() {
        if (payload) {
            free(payload);
            payload = nullptr;
        }
    }
};

class EVENT_BUS_API EventBus {
public:
    static EventBus& instance();
    void  push(const Event& e);
    bool  pop(Event& e);
private:
    EventBus() = default;
    std::queue<Event> data_queue;
    std::mutex        data_mutex;
};
#endif // !EVENT_BUS_H